package com.assignwork.service.impl;

import com.assignwork.dto.AssignWorkDTO;
import com.assignwork.entity.AssignWorkInfo;
import com.assignwork.service.IAssignWorkInfoService;
import com.assignwork.vo.AssignWorkInfoVO;
import com.assignwork.vo.AssignWorkVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.common.cache.EsignCache;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springblade.core.mp.base.BaseServiceImpl;
import com.assignwork.entity.AssignWork;
import com.assignwork.mapper.AssignWorkMapper;
import com.assignwork.service.IAssignWorkService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 二级配工(AssignWork)表服务实现类
 */
@AllArgsConstructor
@Service("assignWorkService")
public class AssignWorkServiceImpl extends BaseServiceImpl<AssignWorkMapper, AssignWork> implements IAssignWorkService {

    IAssignWorkInfoService workInfoService;

    @Override
    public boolean saveOrUpdateAssignWork(BladeUser bladeUser, List<AssignWorkDTO> dtos) {
        for (AssignWorkDTO dto : dtos) {
            Long id = dto.getId();
            if (id != null && id == -1L) {
                id = null;
                dto.setId(null);
            }
            if (id != null) {
                this.workInfoService.disableByAssignId(id);
            }
            this.saveOrUpdate(dto);
            this.workInfoService.saveOrUpdateBatch(dto.getWorkInfoEtt());
            if (
                    !"LOADER_SHIP".equals(dto.getWorkType()) &&
                    !"LOADER_LAND".equals(dto.getWorkType()) &&
                    !"LOADER_OTHER".equals(dto.getWorkType())
            ) {
                int count = this.workInfoService.count(
                        new QueryWrapper<AssignWorkInfo>()
                                .lambda()
                                .eq(AssignWorkInfo::getAssignWorkId, dto.getId())
                                .eq(AssignWorkInfo::getStatus, 1)
                );
                if (count == 0) {
                    this.removeById(dto.getId());
                }
            }
        }
        return false;
    }

    @Override
    public List<AssignWorkVO> listAssignWork(BladeUser bladeUser, AssignWorkDTO dto) {
        List<AssignWorkVO> res = new ArrayList<>();
        List<AssignWork> aw = this.lambdaQuery()
                .eq(AssignWork::getStatus, 1)
                .eq(AssignWork::getPlanId, dto.getPlanId())
                .eq(AssignWork::getShiftKey, dto.getShiftKey())
                .eq(AssignWork::getWorkType, dto.getWorkType())
                .list();

        for (AssignWork assignWork : aw) {
            AssignWorkVO vo = Func.copy(assignWork, AssignWorkVO.class);

            List<AssignWorkInfo> awi = workInfoService.lambdaQuery()
                    .eq(AssignWorkInfo::getAssignWorkId, assignWork.getId())
                    .eq(AssignWorkInfo::getStatus, 1)
                    .list();

            List<AssignWorkInfoVO> r = new ArrayList<>();
            for (AssignWorkInfo assignWorkInfo : awi) {
                r.add(Func.copy(assignWorkInfo, AssignWorkInfoVO.class));
            }

            vo.setWorkInfo(r);

            res.add(vo);
        }

        return res;
    }

    @Override
    public List<AssignWorkVO> listAssignWorkByArrangeId(Long arrangeId) {
        List<AssignWorkVO> res = new ArrayList<>();
        List<AssignWork> aw = this.lambdaQuery()
                .eq(AssignWork::getStatus, 1)
                .eq(AssignWork::getArrangeId, arrangeId)
                .list();

        for (AssignWork assignWork : aw) {
            AssignWorkVO vo = Func.copy(assignWork, AssignWorkVO.class);

            vo.setSign(EsignCache.getSignByUserId(vo.getUpdateUser()));
            List<AssignWorkInfo> awi = workInfoService.lambdaQuery()
                    .eq(AssignWorkInfo::getStatus, 1)
                    .eq(AssignWorkInfo::getAssignWorkId, assignWork.getId())
                    .list();

            List<AssignWorkInfoVO> r = new ArrayList<>();
            for (AssignWorkInfo assignWorkInfo : awi) {
                r.add(Func.copy(assignWorkInfo, AssignWorkInfoVO.class));
            }

            vo.setWorkInfo(r);

            res.add(vo);
        }

        return res;
    }
}
